package com.jdwx.opengl.kotlin

import java.io.File

inline fun <reified A, reified B> Pair<*, *>.asPairOf(): Pair<A, B>? {
    if (first !is A || second !is B) return null
    return first as A to second as B
}

val somePair: Pair<Any?, Any?> = "items" to listOf(1, 2, 3)

val stringToSomething = somePair.asPairOf<String, Any>()
val stringToInt = somePair.asPairOf<String, Int>()
val stringToList = somePair.asPairOf<String, List<*>>()
val stringToStringList = somePair.asPairOf<String, List<String>>() // 破坏类型安全！
class MyClass7 {
    companion object {
        val myClassField1: Int = 1
        var myClassField2 = "this is myClassField2"
        fun companionFun1() {
            println("this is 1st companion function.")
            foo()
        }
        fun companionFun2() {
            println("this is 2st companion function.")
            companionFun1()
        }
    }
    private fun MyClass7.Companion.foo() {
        println("伴随对象的扩展函数（内部）")
    }
    fun test2() {
        MyClass7.foo()
    }
     fun foo() {
        println("-----------")
    }
    init {
        test2()
    }
}
val MyClass7.Companion.no: Int
    get() = 10
fun MyClass7.Companion.foo() {
    println("foo 伴随对象外部扩展函数")
}
fun main(){

    data class Result(val result: Int, val status: Boolean)
    fun function(): Result {
        // 各种计算

        return Result(10, false)
    }
    // 现在，使用该函数：
    val (_, status) = function()

    println( status )

    var yy = null
    val x: String? = yy as String?

    println( "x="+x )

    if(  x ==null ){
        println( "x="+x+",我是null" )
    }
    val something = listOf<String>("1")

    if (something is List) {
        something.forEach { println(it) } // 这些项的类型都是 `Any?`
    }
    println("somePair= "+somePair)
    println("stringToSomething = " + stringToSomething)
    println("stringToInt = " + stringToInt)
    println("stringToList = " + stringToList)
    println("stringToStringList = " + stringToStringList)

    fun readDictionary(file: File): Map<String, *> = file.inputStream().use {
        TODO("Read a mapping of strings to arbitrary elements.")
    }

// 我们已将存有一些 `Int` 的映射保存到该文件
    val intsFile = File("ints.dictionary")

    //this表达式
    class A { // 隐式标签 @A
        inner class B { // 隐式标签 @B
            fun Int.foo() { // 隐式标签 @foo
                val a = this@A // A 的 this
                val b = this@B // B 的 this

                val c = this // foo() 的接收者，一个 Int
                val c1 = this@foo // foo() 的接收者，一个 Int

                val funLit = lambda@ fun String.() {
                    val d = this // funLit 的接收者
                }


                val funLit2 = { s: String ->
                    // foo() 的接收者，因为它包含的 lambda 表达式
                    // 没有任何接收者
                    val d1 = this
                }
            }
        }
    }
    var a = A().B()

    val funLit = lambda@ fun String.() {
        val d = this // funLit 的接收者
    }
    var aa : Int = 10

    fun Int.foo(){
        print( "foo" )
    }

    println( aa.foo() )


//    println("no:${MyClass7.no}")
//    println("no:${MyClass7.foo()}")
//    println("field1:${MyClass7.myClassField1}")
//    println("field2:${MyClass7.myClassField2}")
//    println("field2:${MyClass7.companionFun1()}")
//    println("field2:${MyClass7.companionFun2()}")
//    MyClass7.foo()
    MyClass7.companionFun2()
   // MyClass7().test2();


}